﻿#region itdc

/************************************************************************
*Copyright(C) 2015 by qsgy
 *All rights reserved. 
 *FileName:     $safeitemname$
 *Author:       oyh
 *Version:      
 *UnityVersion：
 *Date:   $time$      
 *Description:    资源加载管理器。单例模式。根据需求的gmobj的路径加载资源
 * 如果加载不了会抛异常。通过属性赋值isLoading相应会加载或者卸载资源。
 *History: 
************************************************************************/
#endregion
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


/// <summary>
/// 动态资源加载管理。
/// 可以动态加载或卸载游戏物体资源
/// 请后期进行unity面板修改。方便循环初始化
/// </summary>
public class ResourceManager : Singleton<ResourceManager> {
    [SerializeField,Tooltip("发射物体资源管理")]//按照shots类中的常数排列
    public Source[] shots=new Source[] { new Source(null,true, "Prefabs/EventObject/Triggers/Particles/Laser")};
    [Header("道具资源")]
    public GameObject[] propSources;

    public override void FixedAwake()
    {
        InitiLoad(shots);
    }

    /// <summary>
    /// 初始化资源
    /// </summary>
    /// <param name="source"></param>
    private void InitiLoad(params Source[][] source)
    {
        for (int i = 0; i < source.Length; i++)
        {
            for (int j = 0; j < source[i].Length; j++)
            {
                source[i][j].InitLoad();
            }
        }
    }


   
}

/// <summary>
/// 一个资源
/// 被加载的是GameObjec。被加载中hasload为true，取消加载了为false
/// </summary>
[System.Serializable]
public struct Source
{
    [SerializeField,Tooltip("被加载的gameobject资源。未被加载是为null")]
    GameObject gmObj;
    [SerializeField,Tooltip("是否在加载中.被修改时，资源也会相应的卸载或者加载")]
    bool isLoading;
    [SerializeField, TextArea, Tooltip("加载的路径.路径被修改时，如果已经有加载了资源，则会自动卸载又加载")]
    string path;

    public Source(GameObject go, bool hasLoad=false, string path="")
    {
        this.gmObj = go;
        this.isLoading = hasLoad;
        this.path = path;
    }


    /// <summary>
    /// 被加载资源只读。
    /// </summary>
    public GameObject GmObj
    {
        get
        {
            return gmObj;
        }
    }

    /// <summary>
    /// 处于加载或者未加载状态。改变状态就会进行加载或者卸载
    /// 暂时private，修复bug
    /// </summary>
    private bool IsLoading
    {
        get
        {
            return isLoading;
        }

        set
        {
            if (isLoading == value) return;//如果一样则不用做修改
            else
            {

                if (value)//加载
                {
                    GameObject gm = Resources.Load<GameObject>(path);
                    if (gm==null)
                    {
                        throw new NullReferenceException("On Path:" + path + " Can't find the GameObj resource");
                    }
                    gmObj = gm;
                }
                else//卸载
                {
                    Resources.UnloadAsset(gmObj);
                    MonoBehaviour.print("gmObj has unloaded "+(gmObj==null));
                }
                isLoading = value;
            }
        }
    }

    /// <summary>
    /// 加载路径。在已经加载情况下，改变路径会卸载原有并加载新的路径
    /// </summary>
    public string Path
    {
        get
        {
            return path;
        }

        set
        {
            if (path!=value)
            {
                path = value;//先改变路径
                if (isLoading)
                {
                IsLoading = false;//先卸载资源再加载资源
                IsLoading = true;
                }
              
            }
        }
    }


    /// <summary>
    /// 对外提供的初始化加载方法,。会自动判断一次加载状态
    /// </summary>
    public void InitLoad()
    {
        if (isLoading)
        {
            Load();
        }
    }

    /// <summary>
    /// 对内的加载方法。
    /// </summary>
    private void Load()
    {
        GameObject gm = Resources.Load<GameObject>(path);
        if (gm == null)
        {
            throw new NullReferenceException("On Path:" + path + " Can't find the GameObj resource");
        }
        gmObj = gm;
    }
}

